package threadcoreknowledge.createthread.threadobjectclasscommommethods;

/**
 * 普通用法，展示wait和notify的基本用法
 * 1.研究代码的执行顺序
 * 2.证明wait释放锁
 */
public class Wait {
        public static Object instance = new Object();

        static class Thread1 extends Thread {
            @Override
            public void run() {
                synchronized (instance){
                    System.out.println("线程1开始执行");

                    try {
                        instance.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程"+Thread.currentThread().getName()+"获取到了锁");
                }
            }
        }

        static class Thread2 extends Thread{
            @Override
            public void run(){
                synchronized (instance){
                    instance.notify();
                    System.out.println("线程"+Thread.currentThread().getName()+"正在执行调用了notify");
                }
            }
        }


    public static void main(String[] args) throws InterruptedException {
            Thread1 thread1 = new Thread1();
            thread1.start();

            Thread.sleep(200);

            Thread2 thread2 = new Thread2();
            thread2.start();

    }


}
